package cn.zh.controller;

import cn.hutool.core.util.IdUtil;
import cn.zh.commons.ResponseResult;
import cn.zh.service.CarouselService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

//轮播图
@Controller
@RequestMapping("carousel")
public class CarouselController {

    @Resource
    private CarouselService carouselService;

    @RequestMapping("front/list")
    @ResponseBody
    public ResponseResult list(){
        return carouselService.list();
    }

    @RequestMapping("front/getSlidePicture")
    public void getSlidePicture(String img, HttpServletResponse response) throws IOException {

        //TODO
        File file = new File("D:/IDEAFrame/upload/slideShow/" + img);
        FileInputStream fileInputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        int copy = IOUtils.copy(fileInputStream, outputStream);
        fileInputStream.close();
        outputStream.close();
    }

    //后台轮播分页数据接口
    @RequestMapping("page")
    @ResponseBody
    public ResponseResult slidePage(Integer page,Integer limit){
        return carouselService.slidePage(page,limit);
    }

    //轮播置顶
    @RequestMapping("edit")
    @ResponseBody
    public ResponseResult editSlide(Integer id){
        return carouselService.editslide(id);
    }

    //删除轮播
    @RequestMapping("delete")
    @ResponseBody
    public ResponseResult deleteSlide(Integer id,String img){
        return carouselService.deleteSlide(id,img);
    }
    //文件上传  获取图片
    @RequestMapping("upload")
    @ResponseBody
    public ResponseResult uploadSlide(MultipartFile file) throws IOException {
        //判定文件是否是空文件
        if (file.isEmpty()){
            return ResponseResult.fail().message("请选择文件");
        }
        //文件上传的步骤
        String originalFilename = file.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);  //png jpg
//        String replace = UUID.randomUUID().toString().replace("-", "");
        String s = IdUtil.simpleUUID();
        //文件在服务器的唯一标识
        String lastName = s+"."+filenameExtension;
        //TODO
        File file1 = new File("D:/IDEAFrame/upload/slideShow/");
        if (!file1.exists()){
            file1.mkdir();
        }

        File target = new File(file1, lastName);

        file.transferTo(target);

        return ResponseResult.success().data(lastName);
    }

    //轮播图片上传
    @RequestMapping("uploadInfo")
    @ResponseBody
    public ResponseResult uploadInfo(String title,String uuid){
        return carouselService.uploadInfo(title,uuid);
    }

}